
namespace SharpTalk.Client.WpfApplication.Converters
{
    using System;
    using System.Globalization;
    using System.Windows.Data;
    using Core.Enums;

    [ValueConversion(typeof(ChatRoomTypeEnum), typeof(String))]
    public class ChatRoomTypeEnumConverter : IValueConverter
    {
        /// <summary>
        /// Converts a value. 
        /// </summary>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <param name="value">The value produced by the binding source.</param><param name="targetType">The type of the binding target property.</param><param name="parameter">The converter parameter to use.</param><param name="culture">The culture to use in the converter.</param>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            switch ((ChatRoomTypeEnum)value)
            {
                case ChatRoomTypeEnum.NotDefined :
                    return Resources.Messages.chatroom_type_not_defined;
                case ChatRoomTypeEnum.Private :
                    return Resources.Messages.chatroom_type_private;
                case ChatRoomTypeEnum.Protected :
                    return Resources.Messages.chatroom_type_protected;
                case ChatRoomTypeEnum.Public :
                    return Resources.Messages.chatroom_type_public;
                case ChatRoomTypeEnum.Private | ChatRoomTypeEnum.Protected :
                    return Resources.Messages.chatroom_type_private_protected;
                case ChatRoomTypeEnum.Public | ChatRoomTypeEnum.Protected :
                    return Resources.Messages.chatroom_type_public_protected;
            }
            return null;
        }

        /// <summary>
        /// Converts a value. 
        /// </summary>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <param name="value">The value that is produced by the binding target.</param><param name="targetType">The type to convert to.</param><param name="parameter">The converter parameter to use.</param><param name="culture">The culture to use in the converter.</param>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(value == null || String.IsNullOrEmpty((string)value))return value;

            if (String.Equals(Resources.Messages.chatroom_type_not_defined, (string)value)) return ChatRoomTypeEnum.NotDefined;
            if (String.Equals(Resources.Messages.chatroom_type_public, (string)value)) return ChatRoomTypeEnum.Public;
            if (String.Equals(Resources.Messages.chatroom_type_private, (string)value)) return ChatRoomTypeEnum.Private;
            if (String.Equals(Resources.Messages.chatroom_type_protected, (string)value)) return ChatRoomTypeEnum.Protected;
            if (String.Equals(Resources.Messages.chatroom_type_private_protected, (string)value)) return ChatRoomTypeEnum.Private | ChatRoomTypeEnum.Protected;
            if (String.Equals(Resources.Messages.chatroom_type_public_protected, (string)value)) return ChatRoomTypeEnum.Public | ChatRoomTypeEnum.Protected;

            return value;
        }
    }
}